package com.pkk.blny.config;

import com.pkk.blny.model.tag.ArticleTagDirective;
import com.pkk.blny.model.tag.CommonTagDirective;
import com.pkk.blny.service.OptionsService;
import com.pkk.blny.service.UserService;
import com.pkk.blny.model.tag.ArticleTagDirective;
import com.pkk.blny.model.tag.CommonTagDirective;
import com.pkk.blny.service.OptionsService;
import com.pkk.blny.service.UserService;
import freemarker.template.TemplateModelException;
import javax.annotation.PostConstruct;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;

/**
 * <pre>
 *     FreeMarker配置
 * </pre>
 *
 * @author : RYAN0UP
 * @date : 2018/4/26
 */
@Slf4j
@Configuration
public class FreeMarkerAutoConfiguration {

  @Autowired
  private freemarker.template.Configuration configuration;

  @Autowired
  private OptionsService optionsService;

  @Autowired
  private UserService userService;

  @Autowired
  private CommonTagDirective commonTagDirective;

  @Autowired
  private ArticleTagDirective articleTagDirective;

  @PostConstruct
  public void setSharedVariable() {
    try {
      //自定义标签
      configuration.setSharedVariable("commonTag", commonTagDirective);
      configuration.setSharedVariable("articleTag", articleTagDirective);
      configuration.setSharedVariable("options", optionsService.findAllOptions());
      configuration.setSharedVariable("user", userService.findUser());
    } catch (TemplateModelException e) {
      log.error("Custom tags failed to load：{}", e.getMessage());
    }
  }
}
